/*
 VideoPlayer
 
 Copyright (c) 2007 Mary Jane Soft - Marlon J. Manrique
 	
 http://mjs.darkgreenmedia.com
 http://marlonj.darkgreenmedia.com
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 $Id: VideoPlayer.pde 261 2007-07-02 20:20:20Z marlonj $
 	
*/

/**
 * VideoPlayer
 *
 * This class is used to control the video display
 */
class VideoPlayer
{
  // The video player state
  final static int STARTED = 1;
  final static int END = 2;
  final static int CLOSED = 3;  

  // The parent MIDlet
  PMIDlet pMIDlet;

  // Current state, began close
  int state = CLOSED;

  // The current video player
  MVideo mVideo;

  /**
   * Create a video player
   *
   * @param pMIDlet the parent MIDlet
   */
  VideoPlayer(PMIDlet pMIDlet)
  {
    this.pMIDlet = pMIDlet;
  }

  /**
   * Draw the component
   */
  void draw()
  {
  }

  /**
   * Check user interaction
   */
  void keyPressed()
  {
    // If another component already consume the key 
    // Do nothing    
    if(keyEventConsumed)
      return;

    // Checks keys
    switch(keyCode)
    {
      // Close the player 
    case FIRE : 
      // If the player is not closed, stop the movie
      // close the player
      if(state != CLOSED)
      {
        keyEventConsumed = true;
        stop();
      }
      break;
    }
  }

  /**
   * Listen events
   */
  void libraryEvent(Object library, int event, Object data)
  {
    if(library == mVideo)
    {
      // Show some information for each event    
      switch(event)
      {
      case MVideo.EVENT_STARTED : 
        message = "Playing Video"; 
        redraw();
        break;
      case MVideo.EVENT_BUFFERING_STARTED :
        message = "Buffering ...";
        redraw();
        break;
      case MVideo.EVENT_BUFFERING_STOPPED :
        message = "";
        redraw();
        break;        
      case MVideo.EVENT_END_OF_MEDIA :  
        state = END;
        message = "";
        redraw();
        break;
      case MVideo.EVENT_STOPPED :       
        message = "";
        redraw();
        break;
      case MVideo.EVENT_ERROR : 
        message = "Error: " + data; 
        redraw();
        break;
      }      
    }
  }

  /**
   * Play the video in the url
   *
   * @param url The video url
   */
  void play(String url)
  {
    // If a video is already in play, stop it
    if(state == STARTED)
      stop();

    // Set message 
    message = "Loading Video"; 

    // Don't draw more to avoid drawing problems 
    noLoop();

    // Start playing the video
    state = STARTED;
    mVideo = new MVideo(pMIDlet,url);
    mVideo.play();
  }

  /**
   * Stop playing the video
   */
  void stop()
  {    
    // If some video is playing
    if(mVideo != null)
    {
      // Stop and close 
      mVideo.stop();
      mVideo.close();
      mVideo = null;
    }

    // Update info and start drawinf again
    state = CLOSED;    
    message = "Play Videos";
    loop();    
  }
}
